package com.zhouxiaoge.realms;

import org.apache.shiro.authc.*;
import org.apache.shiro.realm.Realm;

/**
 * @author 周明阳
 * mail:gqzmy@outlook.com
 * 时间:2018-03-27 17:41
 */

public class MyRealm4 implements Realm {
    @Override
    public String getName() {
        return "myRealm4";
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        /*仅支持UsernamePasswordToke类型的token*/
        return token instanceof UsernamePasswordToken;
    }

    @Override
    public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        /*得到用户名*/
        String principal = (String) token.getPrincipal();
        /*得到密码*/
        String credentials = new String ((char[]) token.getCredentials());

        if (!"li".equals(principal)) {
            throw new UnknownAccountException("用户名不存在！！！");
        }
        if (!"123".equals(credentials)) {
            throw new IncorrectCredentialsException("密码错误");
        }
        /*getName为要访问的Realm名称,即XXX.ini中的*/
        return new SimpleAuthenticationInfo(principal, credentials, getName());
    }
}
